package com.example.calender.ui;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.calender.BaseActivity;
import com.example.calender.BaseApplication;
import com.example.calender.R;
import com.example.calender.persistence.User;
import com.example.calender.persistence.UserDao;
import com.example.calender.utils.DialogOneEditUtils;
import com.example.calender.utils.ToastUtil;

import java.util.concurrent.ScheduledThreadPoolExecutor;

public class ForgetPasswordActivity extends BaseActivity {

    private static final String TAG = "RegisterActivity";
    TextView phoneTextView,messageTextView,passwordTextView,password2TextView,sureTextView;
    private TextView textView3;
    private ScheduledThreadPoolExecutor threadPool;

    @Override
    protected int setLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void findViews() {
        textView3 =(TextView)findViewById(R.id.textView3);
        phoneTextView=(TextView)findViewById(R.id.register_phone_et);
        messageTextView=(TextView)findViewById(R.id.register_message_et);
        passwordTextView=(TextView)findViewById(R.id.register_password_et);
        password2TextView=(TextView)findViewById(R.id.register_password_et_2);
        sureTextView=(TextView)findViewById(R.id.register_sure_tv);
    }

    @Override
    protected void initView() {
        threadPool = ((BaseApplication) getApplication()).getThreadPool();
        textView3.setText("忘记密码");
    }

    @Override
    protected void initListener() {
        //注册
        sureTextView.setOnClickListener(new View.OnClickListener() {

            private UserDao userDao;

            @Override
            public void onClick(View v) {
                String registerphone=phoneTextView.getText().toString();
                String registerpwd=passwordTextView.getText().toString();
                String message = messageTextView.getText().toString().trim() + "";
                String registerpwd2=password2TextView.getText().toString();
                if (TextUtils.isEmpty(registerphone)){
                    ToastUtil.showShort("请输入要找回的用户名");
                }else if (TextUtils.isEmpty(message)){
                    ToastUtil.showShort("密保不能为空!");
                }else if(TextUtils.isEmpty(registerpwd)){
                    ToastUtil.showShort("请输入密码!");
                }else if(registerpwd.length()<6){
                    ToastUtil.showShort("密码不得小于6位");
                }else if(!registerpwd.equals(registerpwd2)){
                    ToastUtil.showShort("两次密码不一致,请重新输入!");
                }else{
                    threadPool.execute(() -> {
                        User user = ((BaseApplication) getApplication()).getUserDB().userDao().findByName(registerphone);
                        runOnUiThread(() ->{
                            if (user != null && message.equals(user.getSecretGuard())) {
                                DialogOneEditUtils.getInitialize(ForgetPasswordActivity.this, new DialogOneEditUtils.DialogClick() {
                                    @Override
                                    public void determineClick() {
                                        finish();
                                    }

                                    @Override
                                    public void cancelClick() {
                                        finish();
                                    }
                                }, "重置成功");
                            } else {
                                ToastUtil.showShort("密保错误");
                            }});
                    });
                }
            }
        });

    }

    @Override
    protected void initData() {

    }
}
